package com.ejoysoft.servlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.*;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.ejoysoft.auth.MD5;

import com.ejoysoft.common.DbConnect;

import java.io.IOException;
import java.io.PrintWriter;

public class LogIn extends HttpServlet implements Servlet
{
	private static final long serialVersionUID = 217251451801586160L;
	
	public LogIn()
	{
	}
	protected void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException {	// TODO Auto-generated method stub
		try {
			
		this.execute(req, resp);
		}catch(Exception e) {
			e.printStackTrace();
			throw new IOException(e.getMessage());
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		this.doGet(req, resp);
	}

	public void execute(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
	{//String  ss = "lyl-xuanlan";
	//String ss1[]=ss.split("-");
	
	//System.out.println("00000000000"+ss1[0]+"  "+ss1[1]);
	
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String name= request.getParameter("username");
		String password= request.getParameter("password");
	    if(name==null||password=="")
		 {
		 response.getWriter().write("false");}        //如果接收失败，返回false
		 else
		 {       //如果接收成功
		 DbConnect dbConnect = new DbConnect();
	 	String sql = "SELECT * " +
		"FROM sys_user WHERE strUserId='" + name + "' ";
		String strSql = "SELECT * " +
		"FROM sys_user WHERE strUserId='" + name + "' and strPWD = '"+ MD5.getMD5ofString(password) +"' ";
		ResultSet re = dbConnect.executeQuery(sql);
		try{
	        if(re!=null&&re.next()){//用户名存在
	        	ResultSet rs = dbConnect.executeQuery(strSql);
	    		try{
	    	        if(rs!=null&&rs.next()){//用户名密码存在
	    	        	response.getWriter().write("true");
	    	        	}
	    	        else{
	    	        	response.getWriter().write("false");
	    	      } 
	    	      }catch (SQLException e) {
	    				e.printStackTrace();
	    			}
	    	    
	        	}
	        else{//用户名不存在
	        	response.getWriter().write("false");
	        	
	        	} 
	        }
		catch (SQLException e) {
				e.printStackTrace();
			}
	    dbConnect.closeCon();
	}
	}
	
}
